/*
 Modulo para modificar la contraseña de el usuario
 Creado por Carlos Pastelin Morales 
 29/12/2014
 */
package Vistas;

import Controlador.Conexion;
import Vistas.Interface;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class modificarpass extends javax.swing.JFrame {
/*Inicializamos las variables que utilizaremos*/
    int id;
    Conexion con;
    ResultSet rs;

    public modificarpass() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTpass = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jTpassN = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jTpassN2 = new javax.swing.JPasswordField();
        jBcambiar = new javax.swing.JButton();
        jBcancelar = new javax.swing.JButton();

        setTitle("Modificar Contraseña");
        setResizable(false);

        jLabel1.setText("Contraseña Actual:");

        jTpass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTpassActionPerformed(evt);
            }
        });

        jLabel2.setText("Contraseña Nueva:");

        jLabel3.setText("Repetir Contraseña:");

        jTpassN2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTpassN2ActionPerformed(evt);
            }
        });

        jBcambiar.setText("Cambiar");
        jBcambiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBcambiarActionPerformed(evt);
            }
        });

        jBcancelar.setText("Cancelar");
        jBcancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBcancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(136, 136, 136)
                        .addComponent(jTpass, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(jBcambiar, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38)
                        .addComponent(jBcancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2)))
                        .addGap(38, 38, 38)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTpassN, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTpassN2, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTpassN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTpassN2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBcambiar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBcancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jTpassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTpassActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTpassActionPerformed

private void jTpassN2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTpassN2ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTpassN2ActionPerformed

private void jBcancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBcancelarActionPerformed
    if (JOptionPane.showConfirmDialog(rootPane, "Estás seguro de cerrarla?", "Cerrar aplicación", 1) == 0) {
        this.dispose();
    }
}//GEN-LAST:event_jBcancelarActionPerformed

private void jBcambiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBcambiarActionPerformed

    if (jTpass.getText().equals("") || jTpassN.getText().equals("") || jTpassN2.getText().equals("")) {

        JOptionPane.showMessageDialog(null, "No puede dejar espacios sin llenar");
    } else {

        String aux = "";
        String Pass = "";
        String oldpass = jTpass.getText();
        String newpass = jTpassN.getText();
        String newpass2 = jTpassN2.getText();

        try {

            // se hace la conexion con la base de datos
            con = new Conexion();

               //metodo para encriptar la contraseña insertada en MD5
            MessageDigest messagedigest = null;

//                    try {
//                        messagedigest = MessageDigest.getInstance("MD5");
//                    } catch (NoSuchAlgorithmException ex) {
//                        Logger.getLogger(alta.class.getName()).log(Level.SEVERE, null, ex);
//                    }
//                
//
//                 messagedigest.update(oldpass.getBytes());
//                String password = new String(messagedigest.digest());
//                
//                messagedigest.update(newpass.getBytes());
//                String newpassword = new String(messagedigest.digest());
                //System.out.println("pass: "+password);
            String consulta = "SELECT  `usuarios`.`password` FROM usuarios WHERE usuarios.Id_usuario = " + id + "";
            String modificar = "UPDATE `usuarios` SET usuarios.password = '" + newpass + "' WHERE usuarios.Id_usuario = " + id + "";
            rs = con.Consulta(consulta);

            while (rs.next()) {
                Pass = rs.getString(1);
            }

            if (Pass.equals(oldpass)) {
                if (newpass.equals(newpass2)) {
                    con.ejecutar(modificar);

                    JOptionPane.showMessageDialog(null, "La contraseña a sido guardada");
                // se compara que el usuario de la base de datos es el mismo que el insertado
                    //se comparan ambas contraseñas encriptadas

                    jTpass.setText("");
                    jTpassN.setText("");
                    jTpassN2.setText("");

                    this.hide();
                } else {
                    JOptionPane.showMessageDialog(null, "La nueva contraseña no coincide con la segunda");

                    jTpass.setText("");
                    jTpassN.setText("");
                    jTpassN2.setText("");

                }
            } else {

                JOptionPane.showMessageDialog(null, "La contraseña actual no coincide");
            }

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
        }

    }


}//GEN-LAST:event_jBcambiarActionPerformed

    public int setId(int id) {
        return id;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(modificarpass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(modificarpass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(modificarpass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(modificarpass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new modificarpass().setVisible(true);
            }
        });

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBcambiar;
    private javax.swing.JButton jBcancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jTpass;
    private javax.swing.JPasswordField jTpassN;
    private javax.swing.JPasswordField jTpassN2;
    // End of variables declaration//GEN-END:variables
}
